<?php

namespace app\home\controller;

use app\home\model\Idfa724Model;
use app\home\model\Order;
use app\home\service\NotifyService;
use app\home\service\OrderService;
use Exception;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
use think\Db;
use think\helper\hash\Md5;
use think\Loader;
use think\Model;
use think\Request;
use think\response\Json;

class Google extends BasePay{
    
    public function __construct()
    {
        header('Content-Type: text/html;charset=utf-8');
        header('Access-Control-Allow-Origin:*'); // *代表允许任何网址请求
        header('Access-Control-Allow-Methods:POST,GET,OPTIONS,DELETE'); // 允许请求的类型
        header('Access-Control-Allow-Credentials: true'); // 设置是否允许发送 cookies
        header('Access-Control-Allow-Headers: Content-Type,Content-Length,Accept-Encoding,X-Requested-with, Origin'); // 
    }

    public function test(){
        Loader::import('jwt.JWT');
        Loader::import('jwt.Key');

        $key = "b3B04jNvivFncwZHFEPTCAnqVfCugYkwo3LWPGWR6jU=";
        $token = 'eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIzMmEzZWZhOGNjZmQ0ZTc0OGZiNzY1ODFkMjE5ODlhYyIsImFwcElkIjoiMTc5NDk4NDk5NDcwODY1NjEyOCIsInVzZXJJZCI6IjMyYTNlZmE4Y2NmZDRlNzQ4ZmI3NjU4MWQyMTk4OWFjIiwic2hvcElkIjoxMDgsImFjY2Vzc1Rva2VuIjoibUFLY0FhbWMvaXptWGFhd0dZWHJZZ1V3ZUtVQncwYU03VnhyNWRtbFhZUjJHdEJTR2dEbkJjQWZ6TU0vajNHcCIsImV4cCI6MTcyMDM0OTk1NH0.e2j6P4fJ4bOJGsxNFie11bCQSXirfJdsZPRDA650vzQ';
 
        try {
            // 解密Token
            // $decoded = JWT::decode($token, $key, array('HS256'));
            $decoded = JWT::decode($token, new Key($key, 'HS256'));
            // 打印解密后的数据
            var_dump(json_encode($decoded));die;
        } catch (Exception $e) {
            // 如果解密失败，可能是因为Token无效或过期
            echo $e->getMessage();die;
        }
        // print_r($decoded);die;
    }
  


    public function token()
    {
        try{
            $params = Request::instance()->param();
            mylog($params);
            $url = 'https://oauth2.googleapis.com/token';
            $data = [
                'client_id'=>'996496061616-1usp1l47dak0523gmb4elripos88jo4m.apps.googleusercontent.com',
                'client_secret'=>'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',
                'code'=>$params['code'],
                'grant_type'=>'authorization_code',
                'redirect_uri'=>'https://www.carcadefishing.com',
            ];
            $res = $this->post($url,$data);
            $res = json_decode($res,true);
            if(empty($res['id_token'])){
                exit(json_encode(['code'=>0]));
            }
            $obj = file_get_contents('https://oauth2.googleapis.com/tokeninfo?id_token='.$res['id_token']);
            mylog($obj);
            exit(json_encode(['code'=>1,'data'=>json_decode($obj,true)]));
        }catch(\Exception $e){
            mylog($e->getMessage().';line'.$e->getLine());
            exit(json_encode(['code'=>0]));
        }
        
    }
    

    /**
     * @param $url
     * @param null $body
     * @return bool|string
     * @throws Exception
     */
    private function post($url, $data)
    {
        $curlHeaders = [
            'content-type: application/x-www-form-urlencoded;CHARSET=utf-8',
        ];
       $data = http_build_query($data);

       $ch = curl_init();
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
       curl_setopt($ch, CURLOPT_URL, $url);
       curl_setopt($ch, CURLOPT_HTTPHEADER, $curlHeaders);
       curl_setopt($ch, CURLOPT_POST, true);
       curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
       //TODO 证书
       curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
       curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
       $response = curl_exec($ch);
       mylog($response);
       
       curl_close($ch);
       return $response;
    }
   
}
